import java.io.Console;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Timestamp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.net.InetAddress;
import java.lang.Object;

public class ReferenceMonitor {
	
	//the set of privileges in the system 
	private final static  String SubmitGrade="SubmitGrade";    			//Submit Grade for a student 
	private final static  String EditGrade ="EG";    			//Edit Grade 
	private final static  String ViewPIILimited="VLPII";  		//View limited TAs' or Students' information in this course.
	private final static  String ViewProfAudit="VPLog";       	//see Audit record for his course.
	private final static  String EditPersonalInfo ="EPII"; 		//edit his PII
	private final static  String AssignTa="AsignTA";			//assign Ta to course.
	private final static  String CreateCourse="CreateCourse";	//create course.
	private final static  String ActiveAccount="AA"; 			// activate user account.
	private final static  String AssignRole="AssignRole";			    // assign principal to course.
	private final static  String ViewGrade="VG"; 	  			//Student see Grade.
	private final static  String ViewAdminLog="ViewAdminLog";
	private final static String system="System";
	private final static  String ViewProfLog="ViewProfLog";
	
	
	Conection 	Con= new Conection();
	Privileges  Priv= new Privileges();
	Role 	    Role=new Role();
	Admin 		admin=new Admin();
	utilities util=new utilities();
//	boolean res;
	String res;
	public static String clientusername=null;
	/*
	 * this function is used to create new course in the data base for a certain semester 
	 */
	public String AddCourse( String Subject ) throws Exception
	{
			res = null;
			String role = Role.GetRole( Subject , "System" );
			// check the role has the create course privilege
			if( Priv.HasPrivilege ( role , CreateCourse ) )
			{
				res = Con.AddCourse( admin.SemList.getSelectedItem( ).toString( ), admin.CourseNumber.getText( ), admin.CourseName.getText( ), admin.CourseHour.getText( ),admin.CourseInstrcuter.getText().toString(),admin.id.toString());
			}
		return ( res );
	}
	
	private byte[] getHash(int iterationNb, String password, byte[] salt) throws NoSuchAlgorithmException, UnsupportedEncodingException {
	    MessageDigest digest = MessageDigest.getInstance("SHA-256");
	    digest.reset();
	    digest.update(salt);
	    byte[] input = digest.digest(password.getBytes("UTF-8"));
	    for (int i = 0; i < iterationNb; i++) {
	        digest.reset();
	        input = digest.digest(input);
	    }
	    return input;
	}
	public String Rigester(String[] line) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
		
		     String res=util.PassWordCheck(line[2]);  
		  // Uses a secure Random not a simple Random
             SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
             // Salt generation 64 bits long
             byte[] salt = new byte[128];
             random.nextBytes(salt);
             byte[] bDigest =   getHash(1000,line[2],salt);
             String password=new String(bDigest);
             String Salt=new String(salt);
             //String sDigest = util.decodeString(newfahad);
             //String sSalt =  util.decodeString(Salt.toString());
            if (res==null)
           	 res=Con.Register(line,password,Salt);
            
            return(res);
    }
	
	public boolean lockout(String username) throws Exception
	{
		boolean res=false;
		res = Con.lockout(username);
		return res;
	}

	
	/*
	 *  this function returns the list of the courses from the data bases
	 */
	public String getCourseNumbers( )
	{
		return ( this.Con.getCourseNumbers ( ) );
	}
	public String getAccountsTable( )
	{
		return ( this.Con.getAccountsTable ( ) );
	}
	
	public String Filtercourselistbyterm( String semester)
	{
		String courselist = this.Con.getCourseNumbersbysemester( semester) ;
		return (courselist);
	}
	
	/*
	 * this function will assign the role to the students and professors to certain course
	 */
	
	public String AssignRole( String Subject ,String newrole,String s) throws Exception
	{
		res = null;
		String roleofthesubject = Role.GetRole( Subject , system );
		if( Priv.HasPrivilege ( roleofthesubject , AssignRole ) )
		{
			if( newrole.equals("Professor") )
			{
				if( ThereIsNoProfessor( admin.courselist.getSelectedItem().toString() , s ) )
				{
					res = this.Con.AssignRole( newrole, admin.courselist.getSelectedItem().toString(),admin.assignedAccountID.getText() , s ,admin.id);
				}
				else
					res="There is already a professor assigned to the course";
			}
			else 
			res = this.Con.AssignRole( newrole,admin.courselist.getSelectedItem( ).toString( ),admin.assignedAccountID.getText() , s ,admin.id);
		}
		return( res );
	}
	public boolean ThereIsNoProfessor(String Course,String Semester)
	{
		boolean res=false;
		res=this.Con.IsThereProfessor(Course,Semester);
		return res;
	}
	
	
	public String GET_PII()
	{
		String  res = null;
		// check the role has the create course privilege
		if( Priv.OwnPIIPr(  clientusername ) )
		{
			res = Con.Get_PII( clientusername );
		}
			return ( res );
	}
	public String get_my_courses(String  id){
		String s=Con.get_my_courses(id);
			return (s);
	}
	public String Get_Students(String[] line)
	{
		// line: Role,username,method,courseID
		String[][] StudentList=null;
		String res=null;
		String role=Role.GetRole(line[1], line[3]);
		if( role.equals("Professor")||role.equals("Ta")||role.equals("Super Ta"))
		{
			StudentList=Con.Get_Students(line);
			res="Students";
			for(int i=0;i<StudentList.length;i++)
			{
				res+=";"+StudentList[i][0]+";"+StudentList[i][1];
				res+=GetStudentPersonalInfo(line[3],StudentList[i][0]);
			}
			if(StudentList.length==0)
			{
				res+=";null";
			}
		}
		
		return (res);
	}
	public String GetStudentPersonalInfo(String courseID,String StudenUsername)
	{
		String res=null;
		if( Priv.IsInCourse(  courseID , StudenUsername ) )
		{
			res=Con.GetLimitedPII(StudenUsername);
		}
		return (res);
	}
	public String Submit_Grade(String[] line)
	{
		String res="update;fail";
		String role=Role.GetRole(clientusername, line[3]);
		if( Priv.HasPrivilege ( role , SubmitGrade ) )
		{
				boolean s=role.equals("Professor");
			if(Con.Submit_Grade(line[3],line[4],line[5],clientusername,admin.clientip,s))
				{
				res="update;done";
				}
		}
		return (res);
	}
	
	public String[][] Get_admin_log(String adminid)
	 {
		 String[][] res=null;
		 String role=Role.GetRole(adminid, system);
			if( Priv.HasPrivilege ( role , ViewAdminLog ) )
			{
				res=Con.Get_admin_log();
			}
		 return(res);
	 }
	 // ################################## Activate Accounts ################################## 
	 public String[][] Get_Users_Accounts(String adminid)
	 {
		 String[][] res=Con.Get_Users_Accounts();
		 return(res);
	 }
	 public String[][] Get_Users_Accountss(String adminid, String username)
	 {
		 String[][] res=Con.Get_Users_Accountss(username);
		 return(res);
	 }
	 public void Update_Accounts(String username, String adminid)
	 {
		 Con.Update_Accounts(username);
		 
	 }
	 public String PriviList(String role)
	 {
		 return(Con.PriviList(role));
	 }
	 public String OwnPII(String subject)
	 {
		 return(Con.OwnPII(subject));
	 }
	 public String PIIPriv(String subject,String Object)
	 {
		 return(Con.PIIPriv(subject, Object));
	 }
	 public String GetRoleRM(String subject,String Object)
	 {
		 return(Con.GetRoleCon(subject, Object));
	 }
	 public String gettranscript(String id)
	 {
		 return(Con.gettranscript(id));
	 }
	 public String GetcourseLog(String[] line)
	 {
		  res=null;
		 String role=Role.GetRole(clientusername, line[3]);
			if( Priv.HasPrivilege ( role , ViewProfLog ) )
			{
				
				res=Con.GetcourseLog(clientusername,line[3]);
			}
		return res;
	 }
	 public String check_authenticated_admin(String username, String password) throws NoSuchAlgorithmException, UnsupportedEncodingException
	 {
		String res1 = "Authenticated";
		String res2 = "Not Authenticated";
		Con.Update_Accounts("admin");
		String[] res = Con.check_authenticated_user(username);
		
		byte[] salt = new byte[128];
		if(res!=null)
		{
        salt=res[2].getBytes();
        byte[] bDigest =   getHash(1000,password,salt);
        String inputpassword=new String(bDigest);
        
		//System.out.println("user: RM" +res_user);
        if(res[1].equals(inputpassword))
        {
			String role = Role.GetRole(username, "System");
			if(role != null){
				
				System.out.println(res1);
				return res1;
			}
			else{
				try{Thread.sleep(1000);  
			       
				   }catch (InterruptedException ie)
					        {
					        System.out.println(ie.getMessage());
					        }
			
				System.out.println(res2);
				return res2;
			}
        }else
        {
        	try{Thread.sleep(1000);  
		       
			   }catch (InterruptedException ie)
				        {
				        System.out.println(ie.getMessage());
				        }
    		return "The Password Does not match";
        }
        
    		}
            else{
            	try{Thread.sleep(1000);  
    		       
    			   }catch (InterruptedException ie)
    				        {
    				        System.out.println(ie.getMessage());
    				        }
            	return "The Password Does not match";
            }
    		
	 }
	 public String check_authenticated_user(String username, String password) throws Exception
	 {
		String[] res = Con.check_authenticated_user(username);
		String result=null;
		byte[] salt = new byte[128];
		if ( res!=null){
        salt=res[2].getBytes();
        byte[] bDigest =   getHash(1000,password,salt);
        String inputpassword=new String(bDigest);
        
        if(res[3].equals("Lockout"))
        {
        	return result="Your Account is Locked out";
        
        }
        	else if(res[1].equals(inputpassword)){
			clientusername=username;
			return result="Success";
		}
    	else if(!res[1].equals(inputpassword)){
    		
    		boolean s=lockout(username);
			return result="The Username and Password don't Match";
    	}
		}
		else
			return result="You're Not a Registered User";
		return result;
	 }

	
private class Conection {
//	public static final InetAddress ip;
	//private String ip=InetAddress.getLocalHost().toString();
	public Connection connection = null;
	public ResultSet resultSet = null;
	public Statement statement = null;
	public PreparedStatement stmt=null;
	private String ip=null;
	Log 		log=new Log();
	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	java.util.Date date = new java.util.Date ();
//    addr.getAddress().toString()
	//Crypto crytop=new Crypto();

	String DataBasePath="jdbc:sqlite:/Users/fahadalfaisal/server.db";
	
	Conection(){
		try{
			ip="asd";
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
	
		
	/*
	 * start () is used to setup the connection with the data base
	 */
	public void start() throws Exception{
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager.getConnection(DataBasePath);

	}
	
	public boolean IsThereProfessor(String Course,String Semester)
	{ 
		boolean res=true;		
		String temp;
		try{
			// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select id from Semesters where SemYear=? and CourseNum=?");
			this.stmt.setString(1, Semester);
			this.stmt.setString(2, Course);
			this.resultSet=this.stmt.executeQuery();
			if ( this.resultSet.next( ) )
			{	
			temp=this.resultSet.getString("id");
			this.stmt=this.connection.prepareStatement("select * from RoleAssign where ClassName=? and Role='Professor'");
			this.stmt.setString( 1 , temp);
			this.resultSet=this.stmt.executeQuery();
			if ( this.resultSet.next() ){
					res=false;
			}
			//this.statement=this.connection.createStatement();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Error from the GetRole");
			}
		}
		return res;
	}
	
	public String GetcourseLog(String prof , String course ){
		String res="courselog;";
			
		try{
				// crytop.Decryption();
				this.start();
				//this.statement=this.connection.createStatement();
				this.stmt=this.connection.prepareStatement("select * from AuditProf where ObjectName=? order by Id desc");
				this.stmt.setString( 1 , course);
				//this.stmt.setString( 2 , course);
				this.resultSet=	this.stmt.executeQuery();
				while (this.resultSet.next()){
				res+=this.resultSet.getString("ActivityTime")+";";
				res+=this.resultSet.getString("ObjectName")+";";
				res+=this.resultSet.getString("SubjectName")+";";
				res+=this.resultSet.getString("ActivityType")+";";
				res+=this.resultSet.getString("ActivityPriorty")+";";
				res+=this.resultSet.getString("ActivityValue")+";";
				res+=this.resultSet.getString("ActivityDesc")+";";

				}
				 if (!this.resultSet.next())
					res+="null";
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("Error from the GetRole");
		
				}
	}
		// crytop.encryption();
	return(res);
	}
	public String OwnPII(String subject){
		String res=null;
		try{
				// crytop.Decryption();
				this.start();
				//this.statement=this.connection.createStatement();
				this.stmt=this.connection.prepareStatement("select * from PIIAssignRole where Privilege='own' and SubjectName=? and ObjectName=?");
				this.stmt.setString( 1 , subject);
				this.stmt.setString( 2 , subject);
				this.resultSet=	this.stmt.executeQuery();
				while (this.resultSet.next()){
				res=this.resultSet.getString("Privilege");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("Error from the GetRole");
		
				}
	}
		// crytop.encryption();
	return(res);
		
	}
	/* 
	 * this function is used to add the course the course table in the database 
	 * add course takes the course info, return true if added else false 
	 */
	public String AddCourse(String SemYear,String CourseNum,String CourseName,String Coursehourse,String CourseInst,String adminid) throws Exception
	{
		String res=null;
		String id=null;
		boolean check=false;
		if (!CourseNum.isEmpty() && !CourseName.isEmpty())
		{
			
		try{
			// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select id from Semesters where SemYear=? and CourseNum=?");
			this.stmt.setString( 1 , SemYear);
			this.stmt.setString( 2 , CourseNum);
			this.resultSet=	this.stmt.executeQuery();
			if (!this.resultSet.next()){
				this.stmt=this.connection.prepareStatement("insert into Semesters (SemYear,CourseNum,CourseName,CourseHour,CourseInstr) values (?,?,?,?,?)");
				this.stmt.setString( 1 , SemYear);
				this.stmt.setString( 2 , CourseNum);
				this.stmt.setString( 3 , CourseName);
				this.stmt.setString( 4 , Coursehourse);
				this.stmt.setString( 5 , CourseInst);
				this.stmt.executeUpdate();
				this.stmt=this.connection.prepareStatement("select id from Semesters where SemYear=? and CourseNum=?");
				this.stmt.setString( 1 , SemYear);
				this.stmt.setString( 2 , CourseNum);
				this.resultSet=	this.stmt.executeQuery();
				id=this.resultSet.getString("id");
				res="the Course is added";
				 check=true;
			}	
		
			}catch(Exception e)
				{
					e.printStackTrace();
					System.out.println("Error from the Addcourse");
				}
		finally {
				try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
					} catch (Exception e) {
						e.printStackTrace();
						}
				}
		}
		// crytop.encryption();
		if ( check) 
			log.CreateCoursesActiivites(SemYear, CourseNum, CourseName, Coursehourse,id , CourseInst, date.toString(), adminid,"normal", "Course added" ,ip.toString());
			
			
		else 
			res="The course already exists";
		return ( res );
	}
	
	/*
	 *	This function returns the privileges for an a Role 
	 */
	public String GetRoleCon(String subject,String object)
	{
		String res=null;
		try{
		// crytop.Decryption();
		this.start();
		this.stmt = this.connection.prepareStatement("select Role from RoleAssign where ClassName=? and SubjectName=?");
		this.stmt.setString(1, object);
		this.stmt.setString(2, subject);
		this.resultSet=	this.stmt.executeQuery();
		while (this.resultSet.next()){
		res=this.resultSet.getString("Role");
		}
	} catch (Exception e) {	
		e.printStackTrace();
	} finally {
		try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Error from the GetRole");
		}
	}
		// crytop.encryption();
	return(res);
	}
	
	public String PriviList(String role)
	{
		String res=null;
		try{
		// crytop.Decryption();
		this.start();
		this.stmt=this.connection.prepareStatement("select SetPrivilges from RoleInfo where RoleName=?");
		this.stmt.setString(1, role);
		this.resultSet=	this.stmt.executeQuery();
		if (this.resultSet.next())
		{
			res=this.resultSet.getString("SetPrivilges");	
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error from the Priv_hasPrivi");

		}
	}
		// crytop.encryption();
	return(res);
	}

	/*
	 * this function retrieve the information from the data base
	 */
	public String getCourseNumbers()
	{
		String res=null;
		String CN="";
			try{
				// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select CourseNum from Semesters");	
			this.resultSet=this.stmt.executeQuery();
			while (this.resultSet.next()) {	
				CN+=this.resultSet.getString("CourseNum")+";";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				}
			}
			// crytop.encryption();
		return (CN);
	}
	
	public String getAccountsTable()
	{
		String CN="";
			try{
				// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select * from accountinfo");
			this.resultSet=this.stmt.executeQuery();
			while (this.resultSet.next()) {	
				CN+=this.resultSet.getString("AccountApproval")+";";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				}
			}
			// crytop.encryption();
		return (CN);
	}
	
	
	public String getCourseNumbersbysemester(String semester)
	{
		String CN="";
			try{
				// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select CourseNum from Semesters where SemYear=?");	
			this.stmt.setString(1, semester);
			this.resultSet=this.stmt.executeQuery();
			while (this.resultSet.next()) {	
				CN+=this.resultSet.getString("CourseNum")+";";
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			} catch (Exception e) {
				e.printStackTrace();
				}
			}
			// crytop.encryption();
		return (CN);
	}
	
	/*
	 * this function is used to assign role to Student or Professor only
	 */
	public String AssignRole(String role,String course,String username,String semester,String adminid) throws Exception
	{	String res=null;
		boolean check=false;
		String temp=null;
	
		 try{	
				//admin.Role.getSelectedItem()
			 // crytop.Decryption();
				this.start();
				this.stmt=this.connection.prepareStatement("select AccountStatus from AccountInfo where UserName=? and AccountStatus='approved'");
				this.stmt.setString(1, username);
				this.resultSet=this.stmt.executeQuery();
				res="The user is not approved yet or not a user";
				if ( this.resultSet.next( ) )
				{
					//res=null;
					this.stmt=this.connection.prepareStatement("select id from Semesters where SemYear=? and CourseNum=?");
					this.stmt.setString(1, semester);
					this.stmt.setString(2, course);
					this.resultSet=this.stmt.executeQuery();
					if ( this.resultSet.next( ) )
					{
						temp=this.resultSet.getString("id");
						this.stmt=this.connection.prepareStatement("Select * from  RoleAssign where ClassName=? and SubjectName=?");
						this.stmt.setString(1, temp);
						this.stmt.setString(2, username);
						this.resultSet=this.stmt.executeQuery();
						if (this.resultSet.next( )==false)
						{
						
						this.stmt=this.connection.prepareStatement("insert into RoleAssign (ClassName,SubjectName,Role) values (?,?,?)");
						this.stmt.setString(1, temp);
						this.stmt.setString(2, username);
						this.stmt.setString(3, role);
						this.stmt.executeUpdate();
						if( role=="Student")
						{
							this.stmt=this.connection.prepareStatement("insert into GradeInfo (CourseNum,StudentId,Grade) values (?,?,?)");
							this.stmt.setString(1, temp);
							this.stmt.setString(2, username);
							this.stmt.setString(3, "null");
							this.stmt.executeUpdate();
							this.stmt=this.connection.prepareStatement("insert into PIIAssignRole values (?,?,?)");
							this.stmt.setString(1, username);
							this.stmt.setString(2, temp);
							this.stmt.setString(3, "ViewLimitedPII");
							this.stmt.executeUpdate();
						}
						check=true;
						}
						else 
							res="The user is already assigned a role in this course.";
					}
					
				
				}
				
				
				
				}
				 catch (Exception e) {
					e.printStackTrace();
					System.out.println("Assign Course Error From the Conection class");
					}
				finally{
					try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
				}catch (Exception e) {
					e.printStackTrace();
					}
			}
		 // crytop.encryption();
		 if(check==false && res.isEmpty())
		 {
			 res="Sorry the user cannot play two roles in the same course";

		 }
		 else if (check)
		 {
			 if( role=="Professor" || role=="Student"){ 
				log.AssignRoleActiivites(temp, username, role,date.toString() , adminid, "normal", "Role is assigned", ip.toString());
				 }
				 else {
					 log.AssignTatoCourseActiivites(temp, username, role, date.toString(), adminid, "normal", "Role is assigned", ip.toString());
				 }
			 res="Role is assigned";
		 }
			return(res);
	}

	/*
	 * this function return the courses that a principal is assigned to 
	 * 
	 */
	public String get_my_courses(String id){
			boolean res=false;
			String s="my_courses";
		try{
			// crytop.Decryption();
				this.start();
				this.stmt=this.connection.prepareStatement("select ClassName,Role,CourseName,CourseHour,CourseNum from RoleAssign,Semesters where SubjectName=? and RoleAssign.ClassName=Semesters.id ");
				this.stmt.setString(1, id);
				this.resultSet=this.stmt.executeQuery();
				while (this.resultSet.next())
				{
				s+=";"+this.resultSet.getString("ClassName");
				s+=";"+this.resultSet.getString("Role");
				s+=";"+this.resultSet.getString("CourseNum");
				res=true;
				}
			}
		 catch (Exception e) {
			e.printStackTrace();
			System.out.println("error in get_my_courses Con class");
			}
		finally{
			try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		}catch (Exception e) {
			e.printStackTrace();
			}
	}
		// crytop.encryption();
	if (res)
		return ( s );
	else
		return ( s+";null" );
	}
	public String Get_PII( String s )
	{	
		String info="my_info";
		try{
			// crytop.Decryption();
				this.start();
				this.stmt=this.connection.prepareStatement("select * from AccountInfo where UserName=?");
				this.stmt.setString(1, s);
				this.resultSet=this.stmt.executeQuery();
				while ( this.resultSet.next( ) )
				{
				info+=";"+this.resultSet.getString("PersonalEmail");
				info+=";"+this.resultSet.getString("PersonalName");
				info+=";"+this.resultSet.getString("PersonalAddress");
				info+=";"+this.resultSet.getString("PersonalPhone");
				}
					
			}
		 catch (Exception e) {
			e.printStackTrace();
			System.out.println("error in get_my_courses Con class");
			}
		finally{
			try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		}catch (Exception e) {
			e.printStackTrace();
			}
	}
		// crytop.encryption();
		return ( info );
	}
	
	/*
	 *  this function is used to get the student list from a certain class for a professor or TAs
	 *
	 */
	public String[][] Get_Students(String[] line)
	{
			// line : P,acountid,Get_Students,courseid
			boolean check=false;
			String[][] StudentList=null;
			try{
				// crytop.Decryption();
				this.start( );
				this.stmt=this.connection.prepareStatement("SELECT COUNT(*) as count FROM GradeInfo where CourseNum=?");
				this.stmt.setString( 1 , line[3] );
				this.resultSet=this.stmt.executeQuery();
				if (this.resultSet.next())
				{
				String length=this.resultSet.getString("count");
				this.stmt=this.connection.prepareStatement("select * from GradeInfo where CourseNum=?");	
				StudentList=new String[Integer.parseInt(length)][2];
				int i=0;
				this.stmt.setString( 1 , line[3] );
				this.resultSet=this.stmt.executeQuery();
				while (this.resultSet.next())
					{
					StudentList[i][0]=this.resultSet.getString("StudentId");
					StudentList[i][1]=this.resultSet.getString("Grade");
					i++;
					}
					check=true;
				}
			}
			 catch (Exception e) {
				e.printStackTrace();
				System.out.println("error from Get_Students in Con");
				}
			finally{
				try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				e.printStackTrace();
				}
			}
			// crytop.encryption();
			return (StudentList);
	}
	
	public String PIIPriv(String subject,String Object){
		String res=null;
		try{
			// crytop.Decryption();
				this.start();
				this.stmt=this.connection.prepareStatement("select * from PIIAssignRole where SubjectName=? and ObjectName=?");
				this.stmt.setString(1, subject);
				this.stmt.setString(2, Object);
				this.resultSet=this.stmt.executeQuery();
				if (this.resultSet.next())
				{
				res=this.resultSet.getString("Privilege");	
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
				} catch (Exception e) {
					e.printStackTrace();
					System.out.println("Error from the GetRole");
		
				}
	}
		// crytop.encryption();
	return(res);
		
	}
	public String GetLimitedPII(String StudenUsername)
	{String res=";";
	try{
		// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select * from AccountInfo where UserName=? ");
			this.stmt.setString(1, StudenUsername );
			this.resultSet=this.stmt.executeQuery();
			while ( this.resultSet.next( ) )
			{
			res+=this.resultSet.getString("PersonalEmail");
			res+=";"+this.resultSet.getString("PersonalName");
			res+=";"+this.resultSet.getString("PersonalPhone");
			}
				
		}
	 catch (Exception e) {
		e.printStackTrace();
		System.out.println("error in get_my_courses Con class");
		}
	finally{
		try {
		this.resultSet.close();
		this.stmt.close();
		this.connection.close();
	}catch (Exception e) {
		e.printStackTrace();
		}
}
	// crytop.encryption();
	return (res);
	}

    public String Register(String[] line,String password,String Salt)
    {
            String res="Rigesteration ";
                    try{
                    	// crytop.Decryption();
                                    this.start();
                                    this.stmt=this.connection.prepareStatement("insert into AccountInfo (UserName,UserPass,Salt,PersonalEmail,PersonalName,PersonalAddress,PersonalPhone,AccountStatus) values (?,?,?,?,?,?,?,?)");      
                                    this.stmt.setString(1,line[1]);
                                    this.stmt.setString(2, password);
                                    this.stmt.setString(3, Salt);
                                    this.stmt.setString(4, line[3]);
                                    this.stmt.setString(5, line[4]);
                                    this.stmt.setString(6, line[5]);
                                    this.stmt.setString(7, line[6]);
                                    this.stmt.setString(8, "Pending");
                                    this.stmt.executeUpdate();
                                    this.stmt=this.connection.prepareStatement("insert into PIIAssignRole values(? ,? ,'own' )");      
                                    this.stmt.setString(1,line[1]);
                                    this.stmt.setString(2, line[1]);
                                    this.stmt.executeUpdate();
                                    res+="done";
                            }
                    catch(Exception e)
                    {
                            res+="fails";
                           e.printStackTrace();
                            System.out.println("Error from the CreateAcount");
                    }
                    try {
                		//this.resultSet.close();
                		this.stmt.close();
                		this.connection.close();
                	}catch (Exception e) {
                		e.printStackTrace();
                		}
                    // crytop.encryption();
            return(res);
    }
	
	
	public boolean Submit_Grade(String course,String Studentusername,String Grade,String Professor,String Professorip,boolean s)
	{	boolean res=false;
		 try{	
				//admin.Role.getSelectedItem()s
			 // crytop.Decryption();	
			 	this.start();
				this.stmt=this.connection.prepareStatement("update GradeInfo set Grade=? where CourseNum=? and StudentId=?");
				//CourseNum varchar(30),StudentId varchar(30),Grade
				this.stmt.setString(1,Grade );
				this.stmt.setString(2,course );
				this.stmt.setString(3,Studentusername );
				this.stmt.executeUpdate();
				res=true;
				}
				 catch (Exception e) {
					e.printStackTrace();
					System.out.println("Assign Course Error From the Conection class");
					}
				finally{
					try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
				}catch (Exception e) {
					e.printStackTrace();
					}
			}
		 // crytop.encryption();
		 if(res)
		 {
			 if (s)
		 	log.SubmitGradeActiivites(course, Studentusername, Grade, date.toString(), Professor, "normal", "the grade is changed", Professorip);	
			 else
			log.SubmitGradeActiivites(course, Studentusername, Grade, date.toString(), Professor, "High", "the grade is changed", Professorip);	

		 }
		 
		 return(res);
	}
	
	public String[][]Get_admin_log()
	{
		String[][]res=null;
		int r=0;
		try{
			// crytop.Decryption();
			this.start();
			this.statement=this.connection.createStatement();
			this.resultSet=this.statement.executeQuery("SELECT COUNT(*) as count from AuditAdmin");
			String length=this.resultSet.getString("count");
			res=new String[Integer.parseInt(length)][8];
			this.resultSet=this.statement.executeQuery("select * from AuditAdmin  order by Id desc");	
			while ( this.resultSet.next( ) )
			{
				res[r][0]=this.resultSet.getString("Category");
				res[r][1]=this.resultSet.getString("ActivityTime");
				res[r][2]=this.resultSet.getString("ObjectName");
				res[r][3]=this.resultSet.getString("SubjectName");
				res[r][4]=this.resultSet.getString("ActivityType");
				res[r][5]=this.resultSet.getString("ActivityPriorty");
				res[r][6]=this.resultSet.getString("ActivityValue");
				res[r][7]=this.resultSet.getString("ActivityDesc");
				r++;
			}
				
		}
	 catch (Exception e) {
		e.printStackTrace();
		System.out.println("error in Get_admin_log Con class");
		}
	finally{
		try {
		this.resultSet.close();
		this.statement.close();
		this.connection.close();
	}catch (Exception e) {
		e.printStackTrace();
		}
}
		// crytop.encryption();
	return (res);
	}
	// ################################### Activate Accounts ##########################################
	public String[][]Get_Users_Accounts()
	{
		String[][]res=null;
		int r=0;
		try{
			// crytop.Decryption();
			this.start();
			this.statement=this.connection.createStatement();
			this.resultSet=this.statement.executeQuery("SELECT COUNT(*) as count from accountinfo where accountstatus='Pending' or accountstatus='Lockout'");
			String length=this.resultSet.getString("count");
			res=new String[Integer.parseInt(length)][9];

			this.resultSet=this.statement.executeQuery("select  username, personalemail, personalname, personaladdress, personalphone, accountstatus from accountinfo where accountstatus='Pending' or accountstatus='Lockout'");	
			
			while ( this.resultSet.next( ) )
			{
				res[r][0]=this.resultSet.getString("UserName");
				res[r][1]=this.resultSet.getString("PersonalEmail");
				res[r][2]=this.resultSet.getString("PersonalName");
				res[r][3]=this.resultSet.getString("PersonalAddress");
				res[r][4]=this.resultSet.getString("PersonalPhone");
				res[r][5]=this.resultSet.getString("AccountStatus");
				r++;
				System.out.println(res);
			}
				
		}
	 catch (Exception e) {
		e.printStackTrace();
		System.out.println("error in Get_admin_log Con class");
		}
	finally{
		try {
		this.resultSet.close();
		this.statement.close();
		this.connection.close();
	}catch (Exception e) {
		e.printStackTrace();
		}
}
		// crytop.encryption();
	return (res);
	}
	
	
	// Search by username
	public String[][]Get_Users_Accountss(String username)
	{
		String[][]res=null;
		int r=0;
		try{
			// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select  username, personalemail, personalname, personaladdress, personalphone, accountstatus from accountinfo where accountstatus='Pending' and username=?");
			this.stmt.setString(1, username);
			this.resultSet=this.stmt.executeQuery();	
			res=new String[Integer.parseInt("0")][0];

			while ( this.resultSet.next( ) )
			{
				res=new String[Integer.parseInt("1")][6];
				res[r][0]=this.resultSet.getString("UserName");
				res[r][1]=this.resultSet.getString("PersonalEmail");
				res[r][2]=this.resultSet.getString("PersonalName");
				res[r][3]=this.resultSet.getString("PersonalAddress");
				res[r][4]=this.resultSet.getString("PersonalPhone");
				res[r][5]=this.resultSet.getString("AccountStatus");
				r++;
				System.out.println(res);
			}
				
		}
	 catch (Exception e) {
		e.printStackTrace();
		System.out.println("error in Get_admin_log Con class");
		}
	finally{
		try {
		this.resultSet.close();
		this.statement.close();
		this.connection.close();
	}catch (Exception e) {
		e.printStackTrace();
		}
}
		// crytop.encryption();
	return (res);
	
	}
	public String gettranscript(String s)
	{
		String res = "mytranscript";
		int count=0;
	//String temp=null;
	try{
		// crytop.Decryption();
	this.start();
	this.stmt=this.connection.prepareStatement("select SemYear,Semesters.CourseNum,CourseName,CourseHour,Grade from GradeInfo,Semesters where Semesters.id = GradeInfo.CourseNum and studentid=?");
	this.stmt.setString(1, s);
	this.resultSet=this.stmt.executeQuery(); 
	while ( this.resultSet.next( ) )
		{
		res+=";<html> <ul> <li><font color=red>"+this.resultSet.getString("SemYear") +"</font></ul>"+ ";" +this.resultSet.getString("coursenum") + ";"+ this.resultSet.getString("CourseName") + ";" + this.resultSet.getString("CourseHour") + ";"+this.resultSet.getString("grade");
		}
	}
	catch (Exception e) {
	e.printStackTrace();
	System.out.println("error in get_my_courses Con class");
	}
	finally{
		try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		}catch (Exception e) {
		e.printStackTrace();
		}
	}
	// crytop.encryption();
	return res;
	}
	public boolean Update_Accounts(String username)
	{
		Date logintime=new Date();
		int temp=0;
		try{	
			// crytop.Decryption();
			this.start();
			String query = "update AccountInfo" +
		    		" set AccountStatus = 'approved' where UserName = ?"; 
			this.stmt=this.connection.prepareStatement(query);
			this.stmt.setString(1, username);
			this.stmt.executeUpdate();
			String Category="user";
			InetAddress ip=null;
				// crytop.Decryption();
			this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
			this.stmt.setString(1, Category);
			this.stmt.setString(2, logintime.toString());
			this.stmt.setString(3, "null");
			this.stmt.setString(4, username);
			this.stmt.setString(5, "Approved");
			this.stmt.setString(6, "Normal");
			this.stmt.setString(7, "The user is trying to login");
			this.stmt.setString(8, "null");
			this.stmt.setString(9, "ip");
			this.stmt.executeUpdate(); 
			}catch(Exception e)
				{
					e.printStackTrace();
					System.out.println("Error from the Update_Accounts");
				}
		finally {
				try {
					this.stmt.close();
					this.connection.close();
					} catch (Exception e) {
						e.printStackTrace();
						}
				}
		if (temp!=0) return true;
		else return false;

	}
	
	public boolean Log_Accounts(String username)
	{
		Date logintime=new Date();
		int temp=0;
		try{	
			// crytop.Decryption();
			this.start();
			String Category="System";
			InetAddress ip=null;
				// crytop.Decryption();
			this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
			this.stmt.setString(1, Category);
			this.stmt.setString(2, logintime.toString());
			this.stmt.setString(3, "null");
			this.stmt.setString(4, username);
			this.stmt.setString(5, "Successful Login");
			this.stmt.setString(6, "Normal");
			this.stmt.setString(7, "The user is successfully loged to the system");
			this.stmt.setString(8, "null");
			this.stmt.setString(9, "ip");
			this.stmt.executeUpdate(); 
			}catch(Exception e)
				{
					e.printStackTrace();
					System.out.println("Error from the Update_Accounts");
				}
		finally {
				try {
					this.resultSet.close();
					this.stmt.close();
					this.connection.close();
					} catch (Exception e) {
						e.printStackTrace();
						}
				}
		if (temp!=0) 
			return true;
		else 
			return false;

	}

	public String[] check_authenticated_user (String username)
	{
		String[] res=new String[4];
		try{
			// crytop.Decryption();
			this.start();
			this.stmt=this.connection.prepareStatement("select username, userpass,Salt,AccountStatus from accountinfo where username=?");
			this.stmt.setString(1, username);
			//this.stmt.setString(2, password);
			this.resultSet=this.stmt.executeQuery();
			if ( this.resultSet.next( ) )
			{
				res[0]=this.resultSet.getString("username");
				res[1]=this.resultSet.getString("userpass");
				res[2]=this.resultSet.getString("Salt");
				res[3]=this.resultSet.getString("AccountStatus");
				
			}else res=null;
				
		}
	 catch (Exception e) {
		e.printStackTrace();
		System.out.println("error in Get_admin_log Con class");
		}
	finally{
		try {
		this.resultSet.close();
		this.stmt.close();
		this.connection.close();
	}catch (Exception e) {
		System.out.println(e);
		}
	}
		// crytop.encryption();
		return res;
	}
	public boolean lockout(String username) throws Exception
	{
		Date logintime=new Date();
		boolean res=false;
		try{
			this.start();
			//this.stmt=this.connection.prepareStatement("select count(*) as count from auditinfo where activitytype='Login Attempt' and subjectname='"+username+"'");
			//this.stmt=this.connection.prepareStatement("select count(*) as count from auditinfo where activitytype='Fail Login Attempt' and subjectname=? order by id");
			
			
			// select count(activityType) from (select activityType,id,subjectname from auditinfo where subjectname='hbargi'  order by id desc limit 3) where activitytype='Login Attemp';
			this.stmt=this.connection.prepareStatement("select count(activityType) as count from (select activityType,id,subjectname from auditinfo where subjectname=?  order by id desc limit 3) where activitytype='Fail Login Attempt'");
			this.stmt.setString(1, username);
			this.resultSet=this.stmt.executeQuery();

			String length=this.resultSet.getString("count");
			int count = Integer.parseInt(length);
			//this.stmt.setString(1, username);
			//this.resultSet=this.stmt.executeQuery();
			
			if ( count == 3 )
			{
				res=true;
					// Deactivate the user account
					
			}
			
			
//			String Category="System";
//			InetAddress ip=null;
//				// crytop.Decryption();
//			this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
//			this.stmt.setString(1, Category);
//			this.stmt.setString(2, logintime.toString());
//			this.stmt.setString(3, "null");
//			this.stmt.setString(4, username);
//			this.stmt.setString(5, "Fail Login Attempt");
//			this.stmt.setString(6, "Normal");
//			this.stmt.setString(7, "The user is trying to login");
//			this.stmt.setString(8, "null");
//			this.stmt.setString(9, "ip");
//			this.stmt.executeUpdate(); 
//			res="less than three attempts";
//			System.out.println("Login attempt is stored in the database");
		
		}
		catch(Exception e){
		      System.out.println("something goes wrong: RM lockout");	
		}try {
			this.resultSet.close();
			this.stmt.close();
			this.connection.close();
		}catch (Exception e) {
			System.out.println(e);
			}
		log.loginattempt(username, res);
	    	
		return res;
		// crytop.encryption();
	}
}

private class Privileges {
	
	public int Principale;

	Privileges()
	{	
	}
		
	/*
	 * this function take two parameters : role, privilege .
	 * return true if the role has the privileges else false 
	 */
	public boolean HasPrivilege(String role, String priv){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=PriviList(role); // it all the privileges that are assigned to a role
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals(priv))
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} 
	
	return(resulte);
	}
	/*
	 * this function checks accessing privileges for personal's personal information
	 */
	public boolean OwnPIIPr(String subject){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=OwnPII( subject );
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "own" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return(resulte);
	}
	
	public boolean IsProfessor(String subject,String course){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=OwnPII(subject); // it all the privileges that are assigned to a role
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "own" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} 
	return(resulte);
	}
	
	public boolean IsInCourse(String subject,String Object){
		boolean resulte=false;
		String res=null;
		String[] privs=null;
		try
		{
			res=PIIPriv(subject,Object); 
			privs=res.split(",");
			for (int i=0;i<privs.length;i++)
			{
				if (privs[i].equals( "ViewLimitedPII" ) )
				{
					resulte=true;
					break;
				}
			}
	} catch (Exception e) {
		e.printStackTrace();
	} 
	return(resulte);
	}
	
}

private class Role {
	
	Role()
	{
		
	}
	
	public String GetRole( String Subject, String Object){
		String role="";
		role=GetRoleRM(Subject, Object);
		return(role);
	}
	
}

private class Log {
	public Connection connection = null;
	public ResultSet resultSet = null;
	public Statement statement = null;
	public PreparedStatement stmt=null;

	String DataBasePath="jdbc:sqlite:/Users/fahadalfaisal/server.db";
	//Crypto crytop=new Crypto();
	Log(){
		
	}
	public void start() throws Exception{
		Class.forName("org.sqlite.JDBC");
		connection = DriverManager.getConnection(DataBasePath);
}

		public boolean LogToSystemActivities(String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress)
		{		
			String ObjectName = "System";  
			String ActivityType="LogIn";
			String Category="System";
			// check if the parameter passed to the method are not empty
			try{
				   // crytop.Decryption();
				   this.start();
				   this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,IpAddress) VALUES (?,?,?,?,?,?,?,?)");
				   this.stmt.setString(1, Category);
				   this.stmt.setString(2, ActivityTime);
				   this.stmt.setString(3, ObjectName);
				   this.stmt.setString(4, UserName);
				   this.stmt.setString(5, ActivityType);
				   this.stmt.setString(6, ActivityPriorty);
				   this.stmt.setString(7, ActivityDesc);
				   this.stmt.setString(8, IpAddress);
				   int InsertResult =this.stmt.executeUpdate(); 
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
			// crytop.encryption();	
			return true;
		}
		// store log out activites in database
 public boolean LogOutFromSystemActivities(String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress){
			
			String ObjectName = "System";  
			String ActivityType="Log Out";
			String Category="System";
			// check if the parameter passed to the method are not empty
			try{
				
				 
				   // crytop.Decryption();
				   this.start();
				   this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,IpAddress) VALUES (?,?,?,?,?,?,?,?)");
				   this.stmt.setString(1, Category);
				   this.stmt.setString(2, ActivityTime);
				   this.stmt.setString(3, ObjectName);
				   this.stmt.setString(4, UserName);
				   this.stmt.setString(5, ActivityType);
				   this.stmt.setString(6, ActivityPriorty);
				   this.stmt.setString(7, ActivityDesc);
				   this.stmt.setString(8, IpAddress);
				   int InsertResult =this.stmt.executeUpdate(); 

			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}	
			// crytop.encryption();
			return true;
		}

		// store Create Course activites in database
		public boolean CreateCoursesActiivites(String SemYear,String CourseNum,String CourseName,String CourseHour,String id,String CourseInstr,String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress){
			
			try{
				String ActivityValue= "Semseter ="+SemYear+" "+"Course Number="+CourseNum+" "+"Course Name="+CourseName+" "+"Course Hour="+CourseHour+" "+"Id="+id+""+"Course Instructor="+CourseInstr;
				String ActivityType="Insert New Course";
				String ObjectName="Semesters";
				String Category="Admin";
					
					// crytop.Decryption();
					this.start();
					this.stmt=this.connection.prepareStatement("insert into AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,IpAddress,ActivityValue) values (?,?,?,?,?,?,?,?,?)");
					   this.stmt.setString(1, Category);
					   this.stmt.setString(2, ActivityTime);
					   this.stmt.setString(3, ObjectName);
					   this.stmt.setString(4, UserName);
					   this.stmt.setString(5, ActivityType);
					   this.stmt.setString(6, ActivityPriorty);
					   this.stmt.setString(7, ActivityDesc);
					   this.stmt.setString(8, IpAddress);
					   this.stmt.setString(9, ActivityValue);
					   int InsertResult =this.stmt.executeUpdate(); 
					   
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}
			try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
			// crytop.encryption();	
			return true;
		}
		// store Assign Role activites in database
		public boolean AssignRoleActiivites(String ClassName,String SubjectName,String Role,String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress){
			
			try{
				String ActivityValue= "SubjectName ="+SubjectName+","+"Role="+Role;
				String ActivityType="Assign New Role";
				String Category="Admin";
			     
			      // crytop.Decryption();
			        this.start();
			        this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,IpAddress,ActivityValue) VALUES "+"(?,?,?,?,?,?,?,?,?)");
					   this.stmt.setString(1, Category);
					   this.stmt.setString(2, ActivityTime);
					   this.stmt.setString(3, ClassName);
					   this.stmt.setString(4, UserName);
					   this.stmt.setString(5, ActivityType);
					   this.stmt.setString(6, ActivityPriorty);
					   this.stmt.setString(7, ActivityDesc);
					   this.stmt.setString(8, IpAddress);
					   this.stmt.setString(9, ActivityValue);
					   int InsertResult =this.stmt.executeUpdate(); 
			
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
						
			// crytop.encryption();
			return true;
		}
		// store Activate Account activites in database
		public boolean ActivateAccountActiivites(String AccountName,String AccountStatus,Timestamp ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress)
		{
			try{
				String ActivityValue= " accountstatus ="+AccountStatus;
				String ActivityType="Activate New Account";
				String Category="Admin";
			    
				 
				  // crytop.Decryption();
				    this.start();
				    this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES(?,?,?,?,?,?,?,?,?)");
					   this.stmt.setString(1, Category);
					   this.stmt.setString(2, ActivityTime.toString());
					   this.stmt.setString(3, AccountName);
					   this.stmt.setString(4, UserName);
					   this.stmt.setString(5, ActivityType);
					   this.stmt.setString(6, ActivityPriorty);
					   this.stmt.setString(7, ActivityDesc);
					   this.stmt.setString(8, ActivityValue);
					   this.stmt.setString(9, IpAddress);
					   int InsertResult =this.stmt.executeUpdate(); 
					
					 
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
						
			// crytop.encryption();
			return true;
		}
		
		public boolean AssignTatoCourseActiivites(String ClassName,String SubjectName,String Role,String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress){
			
			try{
				String ActivityValue= "TA Id = "+SubjectName+","+" Role ="+Role;
				String ActivityType="Assign Ta to Course";
	            String Category="Professor";	
			    
				// crytop.Decryption();
				    this.start();
				    this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,IpAddress,ActivityValue) VALUES (?,?,?,?,?,?,?,?,?)");
					   this.stmt.setString(1, Category);
					   this.stmt.setString(2, ActivityTime.toString());
					   this.stmt.setString(3, ClassName);
					   this.stmt.setString(4, UserName);
					   this.stmt.setString(5, ActivityType);
					   this.stmt.setString(6, ActivityPriorty);
					   this.stmt.setString(7, ActivityDesc);
					   this.stmt.setString(8, IpAddress);
					   this.stmt.setString(9, ActivityValue);
					   int InsertResult =this.stmt.executeUpdate(); 
					 
					 
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
						
			// crytop.encryption();
			return true;
		}
		
		public boolean SubmitGradeActiivites(String CourseNum,String StudentId,String Grade,String ActivityTime,String UserName,String ActivityPriorty,String ActivityDesc,String IpAddress){
			// store Activate Account activites in database
			try{
				String ActivityValue= "StudentId = "+StudentId+","+" Grade ="+Grade;
				String ActivityType="Change In Grade for Student";
				String Category="Professor";
					// crytop.Decryption();
				this.start();
				this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
				this.stmt.setString(1, Category);
				this.stmt.setString(2, ActivityTime.toString());
				this.stmt.setString(3, CourseNum);
				this.stmt.setString(4, UserName);
				this.stmt.setString(5, ActivityType);
				this.stmt.setString(6, ActivityPriorty);
				this.stmt.setString(7, ActivityDesc);
				this.stmt.setString(8, ActivityValue);
				this.stmt.setString(9, IpAddress);
				int InsertResult =this.stmt.executeUpdate(); 
				   
			}
			catch(Exception e){
			      System.out.println("something goes wrong");	
			}try {
				this.resultSet.close();
				this.stmt.close();
				this.connection.close();
			}catch (Exception e) {
				System.out.println(e);
				}
						
			// crytop.encryption();
			return true;
		}
		public void loginattempt(String username, boolean check) throws Exception
		{
			Date logintime=new Date();

			
			 try{	
					this.start();
				 // crytop.Decryption();

				 
				 if(check)
					{
					this.stmt=this.connection.prepareStatement("update accountinfo set AccountStatus='Lockout' where username=?");
					//CourseNum varchar(30),StudentId varchar(30),Grade
					this.stmt.setString(1,username );
					this.stmt.executeUpdate();
					
					String Category="System";
					InetAddress ip=null;
						// crytop.Decryption()
					this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
					this.stmt.setString(1, Category);
					this.stmt.setString(2, logintime.toString());
					this.stmt.setString(3, "null");
					this.stmt.setString(4, username);
					this.stmt.setString(5, "Fail Login Attempt");
					this.stmt.setString(6, "High");
					this.stmt.setString(7, "The user is trying to login");
					this.stmt.setString(8, "null");
					this.stmt.setString(9, "ip");
					this.stmt.executeUpdate(); 
					res="more than three attempts";
					System.out.println("user is flaged to the admin");
					}
					else
					{
						String Category="System";
						InetAddress ip=null;
							// crytop.Decryption();
						this.stmt=this.connection.prepareStatement("INSERT INTO AuditInfo (Category,ActivityTime,ObjectName,SubjectName,ActivityType,ActivityPriorty,ActivityDesc,ActivityValue,IpAddress) VALUES (?,?,?,?,?,?,?,?,?)");
						this.stmt.setString(1, Category);
						this.stmt.setString(2, logintime.toString());
						this.stmt.setString(3, "null");
						this.stmt.setString(4, username);
						this.stmt.setString(5, "Fail Login Attempt");
						this.stmt.setString(6, "Normal");
						this.stmt.setString(7, "The user is trying to login");
						this.stmt.setString(8, "null");
						this.stmt.setString(9, "ip");
						this.stmt.executeUpdate(); 
						res="less than three attempts";
						System.out.println("Login attempt is stored in the database");
					}
					
					}
					 catch (Exception e) {
						e.printStackTrace();
						System.out.println("Assign Course Error From the Conection class");
						}
					finally{
						try {
						this.stmt.close();
						this.connection.close();
					}catch (Exception e) {
						e.printStackTrace();
						}
					}
		
			
			
			
		}
		
		
}


}